// pages/commodity/info.js
var api = require("../../utils/api.js");
var match = require("../../utils/match.js");
var util = require("../../utils/util.js");
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    type: null,
    isExchange:false,
    noEnoughNum:0,
    currentCoin:0,
    tab:[],
    info: {},
    tabIndex:0,
    isPhoneSuccess:false,
    exchangeInfo:{},
    isExchangeSuccess:false,
    startExchange:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id ? options.id : '46',
      type: options.goods_type ? options.goods_type : '1',
      startExchange: options.exchange == 1,
    });

    this.getInfo();
  },

  //获取商品信息
  getInfo: function() {
    wx.showLoading({});
    api.goodInfo(this.data.id, this.data.type, res => {
      wx.hideLoading();
      
      //添加底部tab
      var tabArr = [];
      if(res.data.detail && res.data.detail != ""){
        // 商品介绍页添加
        //宽度满 高度自适应
        var detailInfo = res.data.detail.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
        tabArr.push({
          type:1,
          title:"商品介绍",
        });
        var that = this;
        //显示商品详情
        WxParse.wxParse('detailInfo', 'html', detailInfo, that);
      }
      if(res.order && res.order.length > 0){
        //兑换记录添加
        tabArr.push({
          type:2,
          title:"兑换记录"
        })
      }
      //计算达人币是否足够兑换此物品
      var coin = parseFloat(app.globalData && app.globalData.userStatus && app.globalData.userStatus.point ? app.globalData.userStatus.point : 0);
      this.setData({
        info: res,
        tab:tabArr,
        currentCoin: coin,
        noEnoughNum: coin - parseFloat(res.data.price),
      });
      //是否直接兑换
      if (this.data.startExchange) {
        this.exchange();
      }
      // console.log(this.data.info);
    });
  },

  intercept:function(e){

  },
  //兑换物品
  exchange:function(e){
    if(this.data.info.data.goods_type == 2){
      //虚拟商品兑换
      this.setData({
        isExchange: true,
      });
    }else{
      //实物兑换
      wx.chooseAddress({
        success:res=>{
          this.updateExchangeInfo(res.telNumber,res.userName,res.provinceName+res.cityName+res.countyName+res.detailInfo);
          this.setData({
            isExchange: true,
            isPhoneSuccess:true,
          });
          // console.log(res);
        }
      })
    }
  },

//隐藏达人币不足页面
  hideExchangeNoEnoughView:function(e){
    this.setData({
      isExchange:false,
    });
  },
  //进入凑步数页面
  stepCount:function(e){
    util.toPoster();
  },
//隐藏兑换页面
  hideExchangeView:function(e){
    this.setData({
      isExchange: false,
      isPhoneSuccess:false,
      exchangeInfo:{}
    });
  },
//手机号输入监听
  inputChange:function(e){
    var phone = e.detail.value;
    if (match.isMobile(phone)) {
      wx.showToast({
        title: '请输入正确手机号',
        icon: 'none',
      })
    } else {
      this.updateExchangeInfo(phone);
      this.setData({
        isPhoneSuccess:true,
      });
    }
  },
//输入框唤醒监听
  focusInput:function(e){
    this.setData({
      isPhoneSuccess: false,
    });
  },
//更新兑换信息
  updateExchangeInfo: function (phone, real_name = '', address = '', number = 1){
    var exchangeInfo = {
      id: this.data.info.data.id,
      number: number,
      phone: phone,
      real_name: real_name,
      recharge_number:phone,
      address: address,
      goods_type: 1,
    }
    this.setData({
      exchangeInfo: exchangeInfo,
    });
  },
  //兑换商品
  exchangeGoods:function(e){
    this.toExchange();
  },
  //兑换接口调用并跳转
  toExchange:function(){
    wx.showLoading({
    })
    api.changeGoods(this.data.exchangeInfo,res=>{
      wx.hideLoading();
      //跳转页面
      this.hideExchangeView();
      util.toSponsor('',1);
    },res=>{
      wx.hideLoading();
      wx.showToast({
        title: res,
        icon:'none'
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // return app.shareSeekHelp();
    return app.shareSeekHelp(util.sponsorPath(app.globalData.userStatus.open_id));
  }
})